<template>
  <div class="message">
    <div class="title">
      <div>消息</div>
      <div class="cha" @click="goBack">×</div>
    </div>
    <div class="wrap" v-if="list.length">
      <div class="left" v-infinite-scroll="load" style="min-width: 400px">
        <div
          class="item"
          v-for="item of list"
          :key="item.id"
          @click="look(item)"
        >
          <div class="art">
            <div class="textWrap">
              <div class="only" v-if="!item.readFlag"></div>
              <div class="text">
                {{ item.messageTitle }}
              </div>
            </div>
            <div class="date">{{ item.gmtCreated }}</div>
          </div>
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>
      <div class="right" style="min-width: 600px">
        <div class="content">
          {{ content }}
        </div>
        <div class="date2">{{ time }}</div>
      </div>
    </div>
    <div class="wrap" v-if="!list.length">
      <img class="img" src="@/assets/imgs/middleWork/enw.png" alt="" />
      <div class="imgText">稍安勿燥 暂时还没有任何消息过来</div>
    </div>
  </div>
</template>

<script>
export default {
  name: "middleWorkMessage",
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      content: "",
      time: "",
      list: [],
      hasNextPage: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    goBack() {
      this.$router.back();
    },
    getList() {
      this.$http
        .post("/sys/message/page", {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        })
        .then((res) => {
          if (res.code === "0000") {
            this.list = [...this.list, ...res.data.list];
            this.hasNextPage = res.data?.hasNextPage;
            if (this.pageNum === 1) {
              this.content = this.list[0]?.messageContent || "";
              this.time = this.list[0]?.gmtCreated || "";
            }
          }
        });
    },
    look(item) {
      this.content = item.messageContent;
      this.time = item.gmtModified;
      if (!item.readFlag) {
        this.$http
          .post("/sys/message/saveRead", {
            messageId: item.id,
          })
          .then((res) => {
            if (res.code === "0000") {
              const info = this.list.find((val) => val.id === item.id);
              info.readFlag = true;
              this.$store.commit(
                "changeMsgCount",
                this.$store.state.msgCount - 1
              );
            }
          });
      }
    },
    load() {
      if (this.hasNextPage) {
        this.pageNum += 1;
        this.getList();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.message {
  padding: 20px 30px 0;
  .title {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    font-size: 16px;
    color: #869daa;
    align-items: center;
  }
  .cha {
    font-size: 24px;
    cursor: pointer;
  }
  .wrap {
    display: flex;
    .img {
      position: relative;
      margin: 60px auto;
    }
    .imgText {
      position: absolute;
      left: 860px;
      top: 540px;
    }
    .left {
      width: 513px;
      background: #fff;
      height: calc(100vh - 160px);
      overflow-y: scroll;
      .item {
        // height: 150px;
        padding: 30px;
        border-bottom: 1px solid #f0f0f0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        cursor: pointer;
        .el-icon-arrow-right {
          color: #000;
          font-size: 20px;
          margin-left: 30px;
        }
        .art {
          flex: 1;
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          .textWrap {
            display: flex;
            .text {
              width: 360px;
              margin-bottom: 15px;
              line-height: 22px;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 2;
            }
            .only {
              width: 10px;
              height: 10px;
              background: #44e3b1;
              display: inline-block;
              border-radius: 50%;
              margin-right: 20px;
              margin-top: 10px;
            }
          }

          .date {
            color: #869daa;
            font-size: 12px;
          }
        }
        &:hover {
          background: #f0f0f0;
        }
      }
      .active {
        background: #f0f0f0;
      }
    }
    .right {
      flex: 1;
      margin-left: 20px;
      padding: 60px;
      background: #fff;
      .content {
        line-height: 28px;
        font-weight: 400;
        color: #4f595f;
      }
      .date2 {
        color: #869daa;
        font-size: 12px;
        margin-top: 36px;
      }
    }
  }
}
</style>
